<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Transactions : DataMapper ORM - User Guide</title>

<link rel="shortcut icon" type="image/png" href="../images/favicon.png" />
<link rel="stylesheet" type="text/css" media="all" href="../css/userguide.css" />
<link rel="alternate" type="application/rss+xml" title="Datamapper ORM Updates Feed" href="/rss.xml" />

<meta http-equiv="expires" content="-1" />
<meta http-equiv= 'pragma' content="no-cache" />
<meta name="robots" content="all" />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top"> </a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper ORM</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="/">Datamapper ORM Home</a>  &#8250;
<a href="../index.html">User Guide Home</a>  &#8250;
Validate
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Transactions</h1>

<p>DataMapper handles <strong>transactions</strong> in very much the same way that CodeIgniter does (read CodeIgniter <a href="http://codeigniter.com/user_guide/database/transactions.html">Transactions</a>), obviously because it uses the same methods!  The only real difference is that you'll be calling the transaction methods directly on your DataMapper objects. For example:</p>

<pre class="full">
<samp>// Create user
</samp><var>$u </var><kbd>= new </kbd><var>User</var><kbd>();

</kbd><samp>// Populate with form data
</samp><var>$u</var><kbd>-&gt;</kbd><var>username </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'username'</dfn><kbd>);
</kbd><var>$u</var><kbd>-&gt;</kbd><var>email </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'email'</dfn><kbd>);
</kbd><var>$u</var><kbd>-&gt;</kbd><var>password </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'password'</dfn><kbd>);
</kbd><var>$u</var><kbd>-&gt;</kbd><var>confirm_password </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'confirm_password'</dfn><kbd>);

</kbd><samp>// Begin transaction
</samp><var>$u</var><kbd>-&gt;</kbd><var><u>trans_begin</u></var><kbd>();

</kbd><samp>// Attempt to save user
</samp><var>$u</var><kbd>-&gt;</kbd><var><u>save</u></var><kbd>();

</kbd><samp>// Check status of transaction
</samp><kbd>if (</kbd><var>$u</var><kbd>-&gt;</kbd><var><u>trans_status</u></var><kbd>() === </kbd><var><dfn>FALSE</dfn></var><kbd>)
{
    </kbd><samp>// Transaction failed, rollback
    </samp><var>$u</var><kbd>-&gt;</kbd><var><u>trans_rollback</u></var><kbd>();

    </kbd><samp>// Add error message
    </samp><var>$u</var><kbd>-&gt;</kbd><var><u>error_message</u></var><kbd>(</kbd><dfn>'transaction'</dfn><kbd>, </kbd><dfn>'The transaction failed to save (insert)'</dfn><kbd>);
}
else
{
    </kbd><samp>// Transaction successful, commit
    </samp><var>$u</var><kbd>-&gt;</kbd><var><u>trans_commit</u></var><kbd>();
}

</kbd><samp>// Show all errors
</samp><kbd>echo </kbd><var>$u</var><kbd>-&gt;</kbd><var><i>error</i></var><kbd>-&gt;</kbd><var>string</var><kbd>;

</kbd><samp>// Or just show the transaction error we manually added
</samp><kbd>echo </kbd><var>$u</var><kbd>-&gt;</kbd><var><i>error</i></var><kbd>-&gt;</kbd><var>transaction</var><kbd>;</kbd>
</pre>

<p>If you'd prefer not to have to write the extra transaction code, but would still like to use transactions, then you're in luck!  DataMapper has a <a href="config.html">configuration setting</a> called <var><i>auto_transaction</i></var> which, when set to <dfn>TRUE</dfn>, will automatically wrap your save and delete calls in transactions, even going so far as to give you an error message if the transaction was rolled back.</p>

<p>So, instead of the above, you can do the following and get the same result (provided you've got <var><i>auto_transaction</i></var> set to TRUE of course):</p>

<pre>
<samp>// Create user
</samp><var>$u </var><kbd>= new </kbd><var>User</var><kbd>();

</kbd><samp>// Populate with form data
</samp><var>$u</var><kbd>-&gt;</kbd><var>username </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'username'</dfn><kbd>);
</kbd><var>$u</var><kbd>-&gt;</kbd><var>email </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'email'</dfn><kbd>);
</kbd><var>$u</var><kbd>-&gt;</kbd><var>password </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'password'</dfn><kbd>);
</kbd><var>$u</var><kbd>-&gt;</kbd><var>confirm_password </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'confirm_password'</dfn><kbd>);

</kbd><samp>// Attempt to save user
</samp><kbd>if (</kbd><var>$u</var><kbd>-&gt;</kbd><var><u>save</u></var><kbd>())
{
    </kbd><samp>// Saved successfully
</samp><kbd>}
else
{
    </kbd><samp>// Show all errors
    </samp><kbd>echo </kbd><var>$u</var><kbd>-&gt;</kbd><var><i>error</i></var><kbd>-&gt;</kbd><var>string</var><kbd>;

    </kbd><samp>// Or just show the transaction error
    </samp><kbd>echo </kbd><var>$u</var><kbd>-&gt;</kbd><var><i>error</i></var><kbd>-&gt;</kbd><var>transaction</var><kbd>;
}</kbd>
</pre>

<p class="important"><strong><em>Important:</em></strong>&nbsp; You should check the result of a save() operation. Even if the transaction status indicates that everything went well, the save() could have failed, for example because of a failed validation.</p>

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<span id="footer_previous">Previous Topic:  <a href=""></a>
   &middot;  </span>
<a href="#top">Top of Page</a>   &middot;
<a href="../index.html">User Guide Home</a>
<span id="footer_next">   &middot;
Next Topic:  <a href=""></a></span>
</p>
<div id="copyrights">
<p><a href="/">Datamapper ORM</a> &nbsp;&middot;&nbsp; Copyright &copy; 2010-2011 &nbsp;&middot;&nbsp; Harro "WanWizard" Verton</p>
<p><a href="license.html">Other License Information</a></p>
</div>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagespath: ''
		});

	});
//-->
</script>
</body>
</html>
